package com.qinyeit.serviceapp.utils;

import com.qinyeit.serviceapp.entity.enums.State;
import com.qinyetech.springstage.core.entity.AbstractEntity;
import com.qinyetech.springstage.core.entity.search.SearchFilterHelper;
import com.qinyetech.springstage.core.entity.search.SearchOperator;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.entity.search.filter.SearchFilter;

/**
 * Searchable工具类
 * Created by liyang on 2016/08/03.
 * update by wuqing on 2017/10/27
 */
public class SearchUtils {

    public static SearchFilter getDeletedFilter(){
        return SearchFilterHelper.newCondition("deleted", SearchOperator.eq, false);
    }
    public static SearchFilter getMerchantFilter(Long merchantGroupId){
        return SearchFilterHelper.newCondition("merchantGroupId", SearchOperator.eq,merchantGroupId);
    }
    public static SearchFilter getStateFilter(){
        return SearchFilterHelper.newCondition("state", SearchOperator.eq, State.NORMAL);
    }
    /**
     * 获取 过滤 deleted 后的 查询条件
     * @return
     */
    public static Searchable newSearchableWidthDeletedFilter() {
        Searchable searchable = Searchable.newSearchable();
        return searchable.addSearchFilter(getDeletedFilter());
    }
    /**
     * 获取 过滤 deleted 后的 查询条件
     * @return
     */
    public static Searchable newSearchableWidthMerchantGroupFilter(Long merchantGroupId) {
        Searchable searchable = Searchable.newSearchable();
        return searchable.addSearchFilter(getMerchantFilter(merchantGroupId));
    }



    /**
     * 添加 未删除和商户ID过滤
     * @param searchable
     * @return
     */
    public static Searchable newSearchableWithMerchantFilter(Searchable searchable,Long merchantGroupId){
        searchable.addSearchFilter(getMerchantFilter(merchantGroupId));
        searchable.addSearchFilter(getDeletedFilter());
        return searchable;
    }


    /**
     * 添加 未删除和商户ID过滤
     * @return
     */
    public static Searchable newSearchableWithMerchantFilter(Long merchantGroupId){
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchFilter(getMerchantFilter(merchantGroupId));
        searchable.addSearchFilter(getDeletedFilter());
        return searchable;
    }

    public static SearchFilter timeOccupied(String startName, String endName, String startTime, String endTime){
        return SearchFilterHelper.or(
                SearchFilterHelper.and(
                        SearchFilterHelper.newCondition(startName+"_gte", startTime),
                        SearchFilterHelper.newCondition(startName+"_lte", endTime)
                ),
                SearchFilterHelper.and(
                        SearchFilterHelper.newCondition(endName+"_gte", startTime),
                        SearchFilterHelper.newCondition(endName+"_lte", endTime)
                ),
                SearchFilterHelper.and(
                        SearchFilterHelper.newCondition(startName+"_lte", startTime),
                        SearchFilterHelper.newCondition(endName+"_gte", endTime)
                )
        );
    }

    public static Searchable buildExists(Long id, String key, String value,Long merchantGroupId){
        Searchable searchable = SearchUtils.newSearchableWithMerchantFilter(merchantGroupId);
        if(id!=null && id>0){
            searchable.addSearchFilter("id", SearchOperator.ne, id);
        }
        searchable.addSearchFilter(key, SearchOperator.eq, value);
        return searchable;
    }

    @SuppressWarnings("unchecked")
    public static Searchable existsWithDeletedFilter(Object entity, String field, Object value){
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchParam("deleted_eq", false);
        if(entity!=null && entity instanceof AbstractEntity){
            AbstractEntity obj = (AbstractEntity) entity;
            if(obj.getId()!=null && obj.getId()!=0L){
                searchable.addSearchParam("id_ne", obj.getId());
            }
        }
        searchable.addSearchParam(field+"_eq", value);
        return searchable;
    }
    public static Searchable existsWithDeletedFilter(String field, Object value){
        return existsWithDeletedFilter(null,field,value);
    }
}
